package org.ichow.scenegame.notis
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import org.ichow.debug.Log;
	import org.ichow.scenegame.core.CScrollBar;
	import org.ichow.scenegame.core.Item;
	import org.ichow.scenegame.events.NotiEvent;
	
	/**
	 * ...
	 * @author M.ichow
	 */
	public class EndNoti extends BaseNoti
	{
		
		//end
		private var _ways : Array;
		private var _item : Class;
		//
		private var __titleMc : MovieClip;
		private var __wayMc : Sprite;
		private var __menuBtn : SimpleButton;
		private var __againBtn : SimpleButton;
		private var __scoreTf : TextField;
		private var __maxScoreTf : TextField;
		private var __countTf : TextField;
		private var __wayInfoTf : TextField;
		private var __wayScrollBar : Sprite;
		private var __wayMaskMc:Sprite;
		private var wayScroll:CScrollBar;
		private var maxWay : int = 4;
		//list
		private var __listMc : Sprite;
		private var __listWayMc : Sprite;
		private var __listScrollBar : Sprite;
		private var __listMaskMc:Sprite;
		private var __listCloseBtn : SimpleButton;
		private var listScroll:CScrollBar;
		
		public function EndNoti()
		{
			super();
		}
		
		override protected function init() : void
		{
			super.init();
			
			var end : Sprite = _noti as Sprite;
			
			__titleMc = end.getChildByName( "title_mc" ) as MovieClip;
			__wayMc = end.getChildByName( "way_mc" ) as Sprite;
			__wayInfoTf = end.getChildByName( "info_txt" ) as TextField;
			__scoreTf = end.getChildByName( "score_txt" ) as TextField;
			__maxScoreTf = end.getChildByName( "maxScore_txt" ) as TextField;
			__countTf = end.getChildByName( "count_txt" ) as TextField;
			__againBtn = end.getChildByName( "again_btn" ) as SimpleButton;
			__menuBtn = end.getChildByName( "menu_btn" ) as SimpleButton;
			__wayMaskMc = end.getChildByName("mask_mc") as Sprite;
			__wayScrollBar = end.getChildByName("scrollBar_mc") as Sprite;
			//list
			__listMc = end.getChildByName( "list_mc" ) as Sprite;
			__listCloseBtn = __listMc.getChildByName( "close_btn" ) as SimpleButton;
			__listMaskMc = __listMc.getChildByName("mask_mc") as Sprite;
			__listScrollBar = __listMc.getChildByName( "scrollBar_mc" ) as Sprite;
			__listWayMc = __listMc.getChildByName( "way_mc" ) as Sprite;
			//set
			__menuBtn.addEventListener( MouseEvent.CLICK , onMenuMouseClick );
			__againBtn.addEventListener( MouseEvent.CLICK , onAgainMouseClick );
			
			__listMc.visible = false;
			__listCloseBtn.addEventListener( MouseEvent.CLICK , onListCloseMouseClick );
			
		}
		
		private function onListCloseMouseClick( e : MouseEvent ) : void
		{
			__listMc.visible = false;
		}
		
		private function setEndInfo( obj : Object = null ) : void
		{
			if ( !obj )
			{
				Log.trace( "@@更新数据错误: " + obj );
				return;
			}
			__titleMc.gotoAndStop( obj.score < 100 ? 2 : 1 );
			// clear way mc
			while ( __wayMc.numChildren > 0 )
			{
				__wayMc.removeChildAt( 0 );
			}
			while ( __listWayMc.numChildren > 0 )
			{
				__listWayMc.removeChildAt( 0 );
			}
			//create way
			__menuBtn.visible = false;
			
			var wayInfo : String = "";
			if ( ways )
			{
				for ( var i : int = 0 , len : int = ways.length ; i < len ; i++ )
				{
					if ( i < maxWay )
					{
						if ( ways[ i ] )
						{
							var w : Sprite = getWay( i , ways[ i ].title );
							__wayMc.addChild( w );
						}
					}
					if ( len > maxWay )
					{
						if ( ways[ i ] )
						{
							var lw : Sprite = getWay( i , ways[ i ].title );
							__listWayMc.addChild( lw );
						}
					}
					wayInfo += "第 " + int( i + 1 ).toString() + " 步选择 " + ways[ i ].title + " " + ways[ i ].endValue;
				}
				if ( len > maxWay )
				{
					listScroll = new CScrollBar( __listMaskMc , __listWayMc , __listScrollBar ,  CScrollBar.VERTICAL , true );
					listScroll.height = __listMaskMc.height;
					__menuBtn.visible = true;
				}
			}
			//set info tf
			__scoreTf.text = obj.score;
			__maxScoreTf.text = obj.maxScore;
			__countTf.text = obj.count;
			__wayInfoTf.text = "";
			__wayInfoTf.autoSize = "left";
			__wayInfoTf.multiline = true;
			__wayInfoTf.wordWrap = true;
			__wayInfoTf.mouseWheelEnabled = false;
			__wayInfoTf.text = wayInfo;
			//
			
			wayScroll = new CScrollBar( __wayMaskMc , __wayInfoTf , __wayScrollBar , CScrollBar.VERTICAL , true );
			wayScroll.height = __wayMaskMc.height;
		}
		
		override public function update( o : Object = null ) : void
		{
			super.update( o );
			if ( o.toolsInfo )
				setEndInfo( o.toolsInfo );
		}
		
		private function onMenuMouseClick( e : MouseEvent ) : void
		{
			// show way list.
			__listMc.visible = true;
		}
		
		private function onAgainMouseClick( e : MouseEvent ) : void
		{
			// restart the game
			var evt : NotiEvent = new NotiEvent( NotiEvent.NOTI_CHANGE );
			evt.status = NotiEvent.INIT;
			dispatchEvent( evt );
			cancel();
		}
		
		private function getWay( count : int , title : String = "" ) : Sprite
		{
			var way : Sprite = new item() as Sprite;
			var countTf : TextField = way.getChildByName( "count" ) as TextField;
			var titleTf : TextField = way.getChildByName( "title" ) as TextField;
			//
			countTf.text = "第 " + int( count + 1 ).toString() + " 步";
			titleTf.text = title;
			//
			way.x = ( count % 2 ) * ( way.width + 2 );
			way.y = Math.floor( count / 2 ) * ( way.height + 2 );
			//
			return way;
		}
		
		public function get item() : Class
		{
			return _item;
		}
		
		public function set item( value : Class ) : void
		{
			_item = value;
		}
		
		public function get ways() : Array
		{
			return _ways;
		}
		
		public function set ways( value : Array ) : void
		{
			_ways = value;
			Log.trace( "路线: " + value );
		}
	
	}

}